package com.elvende.web.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Elvende implements EntryPoint {
	 // interface Binder extends UiBinder<HTMLPanel, Elvende> { }
	 // private static final Binder binder = GWT.create(Binder.class);	
	 
	  public void onModuleLoad() {
		    // set uncaught exception handler
		    GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
		      public void onUncaughtException(Throwable throwable) {
		        String text = "Uncaught exception: ";
		        while (throwable != null) {
		          StackTraceElement[] stackTraceElements = throwable.getStackTrace();
		          text += throwable.toString() + "\n";
		          for (int i = 0; i < stackTraceElements.length; i++) {
		            text += "    at " + stackTraceElements[i] + "\n";
		          }
		          throwable = throwable.getCause();
		          if (throwable != null) {
		            text += "Caused by: ";
		          }
		        }
		        DialogBox dialogBox = new DialogBox(true);
		        DOM.setStyleAttribute(dialogBox.getElement(), "backgroundColor", "#ABCDEF");
		        System.err.print(text);
		        text = text.replaceAll(" ", "&nbsp;");
		        dialogBox.setHTML("<pre>" + text + "</pre>");
		        dialogBox.center();
		      }
		    });

		    // use a deferred command so that the handler catches onModuleLoad2() exceptions
		    DeferredCommand.addCommand(new Command() {
		      public void execute() {
		        onModuleLoad2();
		      }
		    });
		  }
	 
	 
	public void onModuleLoad2() {
		// HTMLPanel outer = binder.createAndBindUi(this);		
		
	    HandlerManager eventBus = new HandlerManager(null);
	    AppController appViewer = new AppController(eventBus);
	    appViewer.go(RootLayoutPanel.get());			    
	    
	    // RootPanel.get().add(outer);			
	  }
}
